package com.housekeeping.housekeeping.administrator.adminsclassify.dao.impl;

import com.housekeeping.housekeeping.administrator.adminsclassify.dao.ClassifyDao;
import com.housekeeping.housekeeping.administrator.entity.AdminsClassify;
import com.housekeeping.housekeeping.administrator.entity.AdminsInformation;
import com.housekeeping.housekeeping.administrator.utils.Dbmanage;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ClassifyDaoImpl implements ClassifyDao {
    @Override
    public List<AdminsClassify> Query(int offset, int limit) {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        List<AdminsClassify> list=new ArrayList<>();
        try {
            connection=Dbmanage.getConnection();
            String sql="select *from admins_classify limit ? offset ?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,limit);
            statement.setInt(2,offset);
            resultSet=statement.executeQuery();
            while (resultSet.next()){
                AdminsClassify classify = new AdminsClassify();
                classify.setId(resultSet.getInt(1));
                classify.setStation(resultSet.getString(2));
                classify.setCount(resultSet.getInt(3));
                list.add(classify);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            Dbmanage.close(connection,statement,resultSet);
        }
        return list;
    }

    @Override
    public int selectCount() throws SQLException {
        Connection connection = Dbmanage.getConnection();
        String sql = "SELECT COUNT(*)  FROM admins_classify";
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();

        int total = rs.next() ? rs.getInt(1) : 0;
        Dbmanage.close(ps,rs,connection);
        return total;
    }

    @Override
    public int delete(int id) {
        Connection connection=null;
        Statement statement=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        AdminsClassify adminsClassify = new AdminsClassify();
        int count=0;
        try {
            connection=Dbmanage.getConnection();
            String sql="select *from admins_classify where id="+id;
            preparedStatement=connection.prepareStatement(sql);
             resultSet= preparedStatement.executeQuery();
             while (resultSet.next()){
                 adminsClassify.setCount(resultSet.getInt(3));
             }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            Dbmanage.close(connection,statement,preparedStatement,resultSet);
        }
        if (adminsClassify.getCount()==0) {
            try {
                connection = Dbmanage.getConnection();
                String sql = "update admins_classify SET is_del=1 where id=" + id;
                statement = connection.createStatement();
                count = statement.executeUpdate(sql);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                Dbmanage.close(connection, statement);
            }
            return count;
        }
        else {
            return count;
        }

    }


}
